<?php
class Order_Estimator
{
	private $orderManager;
	public function Order_Estimator($orderManager)
	{
		$this->orderManager = $orderManager;
	}
	public function estimate()
	{
		$orderInfo = $this->orderManager->getInfo();
		$catalogSetting = $this->getCatalogSetting();
		
		$priceTable = array();
		$totalPrice = 0;
		$discountPrice = 0;
		//catalog price
		$priceTable['catalog'] = $catalogSetting['price'];
		$totalPrice += $priceTable['catalog'];
		//disk
		$priceTable['add_disk'] = $orderInfo['disk_number_add']*Util::getGlobalValue(constant('WEBSITE_CFG_BENZU_1'));
		$priceTable['add_disk_number'] = $orderInfo['disk_number_add'];
		$totalPrice += $priceTable['add_disk'];
		//benzu_customize_price
		$priceTable['benzu_customize_price'] = $orderInfo['benzu_customize_price'] - $catalogSetting['benzu_customize_price'];
		$totalPrice += $priceTable['benzu_customize_price'];
		
		//benzu style
		$price = 0;
		//$priceTable['benzu_style'] = array();
		$orderBenzuStyleNameList = $orderInfo['benzu_style'];
		$orderBenzuStyleNameList = preg_split('/,/',$orderBenzuStyleNameList);
		$catalogBenzuStypeName = $catalogSetting['benzu_style'];
		$dtBenzuStyle = new DataTable(constant('DB_TABLE_BENZU_STYLE'));
		$priceTable['benzu_style_number'] = 0;
		foreach($orderBenzuStyleNameList as $tmpData)
		{
			if($tmpData != $catalogBenzuStypeName)
			{
				$benzuStyleData = $dtBenzuStyle->get_row(sprintf("`name`='%s'",$tmpData));
				if($benzuStyleData)
				{
					//$priceTable['benzu_style'][] = array('name'=>$benzuStyleData['name'],'price'=>$benzuStyleData['price']);
					$price += $benzuStyleData['price'];
					$priceTable['benzu_style_number']++;
				}
			}
		}
		$priceTable['benzu_style'] = $price;
		$totalPrice += $price;
		//photo person
		$priceTable['add_person'] = $orderInfo['person_number_add']*Util::getGlobalValue(constant('WEBSITE_CFG_BENZU_3'));
		$priceTable['add_person_number'] = $orderInfo['person_number_add'];
		$totalPrice += $priceTable['add_person'];
		//photo person child
		$priceTable['add_person2'] = $orderInfo['person_number2_add']*Util::getGlobalValue(constant('WEBSITE_CFG_BENZU_33'));
		$priceTable['add_person2_number'] = $orderInfo['person_number2_add'];
		$totalPrice += $priceTable['add_person2'];
		//dress
		$priceTable['add_dress'] = $orderInfo['dress_number_add']*Util::getGlobalValue(constant('WEBSITE_CFG_BENZU_2'));
		$priceTable['add_dress_number'] = $orderInfo['dress_number_add'];
		$totalPrice += $priceTable['add_dress'];
		//dress
		$priceTable['add_dress2'] = $orderInfo['dress_number2_add']*Util::getGlobalValue(constant('WEBSITE_CFG_BENZU_22'));
		$priceTable['add_dress2_number'] = $orderInfo['dress_number2_add'];
		$totalPrice += $priceTable['add_dress2'];
		//zoom size
		//$priceTable['zoom'] = array();
		$orderZoomList = $orderInfo['zoom_list'];
		
		$price = 0;
		$priceTable['zoom_number'] = 0;
		
		if(is_array($orderZoomList) && count($orderZoomList) > 0 && !is_string($orderZoomList[0]))
		{
		$catalogZoomList = array();
		foreach($catalogSetting['photo_size'] as $tmpData)
		{
			$catalogZoomList[$tmpData->size_id] = $tmpData->number;
		}
		if(count($orderZoomList) > 0)
		{
			$dtPhotoSize = new DataTable(constant('DB_TABLE_PHOTO_SIZE'));
			foreach($orderZoomList as $tmpData)
			{
				if(isset($catalogZoomList[$tmpData['size_id']]))
				{
					$tmpData['number'] -= $catalogZoomList[$tmpData['size_id']];
				}
				if($tmpData['number'] > 0)
				{
					$sizeData = $dtPhotoSize->get_row(sprintf("`name`='%s'",Util::sql_safe($tmpData['size_name'])));
					if($sizeData)
					{
						if($tmpData['is_gift'] != 'Y')
						{
							$price += floatVal($sizeData['price'])*intVal($tmpData['number']);
							
						}else{
							$discountPrice += floatVal($sizeData['price'])*intVal($tmpData['number']);
						}
						$priceTable['zoom_number']++;
					}
				}
			}
		}
		}
	
		$priceTable['zoom'] = $price;
		$totalPrice += $price;
		
		//zoom frame 
		$price = 0;
		
		$orderZoomFrameList = $orderInfo['zoom_frame_list'];
		if(is_array($orderZoomFrameList) && count($orderZoomFrameList) > 0 && !is_string($orderZoomFrameList[0]))
		{
		$catalogZoomFrameMap = $catalogSetting['photo_size_frame'];
		$dtPhotoFramePrice = new DataTable(constant('DB_TABLE_PHOTO_FRAME_PRICE'));
		$priceTable['zoom_frame_number'] = 0;
		foreach($orderZoomFrameList as $tmpData)
		{
			$catalogSizeFrameList = $catalogZoomFrameMap->$tmpData['size_id'];
			$containInCatalog = false;
			if($catalogSizeFrameList)
			{
			foreach($catalogSizeFrameList as $tmpData2)
			{
				if($tmpData2->frame_id == $tmpData['frame_id'])
				{
					$containInCatalog = true;
					break;
				}
			}
			}
			
			if(!$containInCatalog)
			{
				$photpFrameData = $dtPhotoFramePrice->get_row(sprintf("`photo_size_id`=%d and `photo_frame_id`=%d",$tmpData['size_id'],$tmpData['frame_id']));
				if($photpFrameData)
				{
					if($tmpData['is_gift'] != 'Y')
					{
						$price += floatVal($photpFrameData['price']);
					}else{
						$discountPrice += floatVal($photpFrameData['price']);
					}
					$priceTable['zoom_frame_number']++;
				}
			}
		}
		}
		
		$priceTable['zoom_frame'] = $price;
		$totalPrice += $price;
		//gift
		$price = 0;
		
		$orderGiftList = $orderInfo['gift_list'];
		$priceTable['gift_detail'] = array();
		if(is_array($orderGiftList) && count($orderGiftList) > 0 && !is_string($orderGiftList[0]))
		{
		$catalogGiftList = $catalogSetting['gift'];
		
		$dtGift = new DataTable(constant('DB_TABLE_GIFT'));
		foreach($orderGiftList as $tmpData)
		{
			foreach($catalogGiftList as $tmpData2)
			{
				if($tmpData['gift_id'] == $tmpData2->gift_id)
				{
					$tmpData['number'] -= $tmpData2->number;
					break;
				}
			}
			if($tmpData['number'] > 0)
			{
				$giftData = $dtGift->get($tmpData['gift_id']);
				if($giftData)
				{
					$itemPrice = floatVal($giftData['price'])*intVal($tmpData['number']);
					if($tmpData['is_gift'] != 'Y')
					{
						$price += $itemPrice;
							
					$priceTable['gift_detail'][] = array('name'=>$giftData['name'],'number'=>intVal($tmpData['number']),'price'=>$itemPrice,'is_gift'=>$tmpData['is_gift'] );

	
					}else{
						$discountPrice += $itemPrice;
					}
	
				}
			}
		}
		}
		
		$priceTable['gift'] = $price;
		$totalPrice += $price;
		$priceTable['total'] = $totalPrice;
		$priceTable['discountPrice'] = $discountPrice;
		
		return $priceTable;
	}
	public function getCatalogSetting()
	{
		$setting = array();
		$info = $this->orderManager->getInfo();
		$catalogName = $info['catalog'];
		$dtCatalog = new DataTable(constant('DB_TABLE_CATALOG'));
		$catalogData = $dtCatalog->get_row(sprintf("`name`='%s'",Util::sql_safe($catalogName)));
		if($catalogData  == null)
			throw new Exception("catalog ".$catalogName." not exist");
		$setting['price'] = $catalogData['price'];
		$setting['disk_number'] = $catalogData['disk_number'];
		$setting['benzu_number'] = $catalogData['benzu_number'];
		$dtBenzuStyle = new DataTable(constant('DB_TABLE_BENZU_STYLE'));
		$benzuStyleData = $dtBenzuStyle->get($catalogData['benzu_style_id']);
		if($benzuStyleData)
		{
			$setting['benzu_style'] = $benzuStyleData['name'];
		}else{
			$setting['benzu_style'] = '';
		}
		
		$setting['person_number'] = $catalogData['person_number'];
		$setting['dress_number'] = $catalogData['dress_number'];
		$catalogHelper = new Catalog_Helper();
		$catalogHelper->init($catalogData['id']);
		$helperData = $catalogHelper->getInfo();
		$setting['photo_size'] = $helperData['photo_size'];
		$setting['photo_size_frame'] = $helperData['photo_size_frame'];
		$setting['gift'] = $helperData['gift'];
		
		$setting['benzu_customize_price'] = $catalogData['benzu_customize_price'];
		return $setting;
	}
}
?>